﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using PearTunes.Client.PearTunesServiceReference;

namespace PearTunes.Client
{
    public delegate void NewPlaylistAddedEventHandler(object sender, EventArgs e);

    public partial class LocalMusicPage : Page
    {
        PlayListManager playListManager;
        public event NewPlaylistAddedEventHandler NewPlaylistAdded;

        public LocalMusicPage(PlayList playList)
        {
            InitializeComponent();
            musicDataGrid.ItemsSource = playList;
        }

        public LocalMusicPage(ref PlayListManager playListManager)
        {
            InitializeComponent();
            this.playListManager = playListManager;
            musicDataGrid.ItemsSource = this.playListManager.GetLocalPlayList();
            CreateMenu();
        }

        protected virtual void OnNewPlaylistAdded(EventArgs e)
        {
            if (NewPlaylistAdded != null)
            {
                NewPlaylistAdded(this, e);
            }
        }

        private void CreateMenu()
        {
            ContextMenu addToListMenu = new ContextMenu();
            musicDataGrid.ContextMenu = addToListMenu;
            MenuItem availableLists = new MenuItem() { Header = "Hozzáadás meglévő lejátszási listához" };
            MenuItem createNewList = new MenuItem() { Header = "Hozzáadás új lejátszási listához"};
            
            if (playListManager.UserPlayListCount == 0)
            {
                availableLists.IsEnabled = false;
            }
            else
            {
                for (int i = 0; i <= playListManager.UserPlayListCount - 1; i++)
                {
                    MenuItem localPlaylistItem = new MenuItem() { Header = playListManager.GetUserPlayListNameAt(i) };
                    localPlaylistItem.Click += new RoutedEventHandler(localPlaylistItem_Click);
                    availableLists.Items.Add(localPlaylistItem);
                }
            }

            createNewList.Click += new RoutedEventHandler(createNewList_Click);
            addToListMenu.Items.Add(availableLists);
            addToListMenu.Items.Add(createNewList);
            
        }

        void localPlaylistItem_Click(object sender, RoutedEventArgs e)
        {
            playListManager.AddToUserPlayList((sender as MenuItem).Header.ToString(), (LocalTrack)musicDataGrid.SelectedItem);
        }

        void createNewList_Click(object sender, RoutedEventArgs e)
        {
            NewPlaylistForm form = new NewPlaylistForm();
            if (form.ShowDialog() == true)
            {
                playListManager.AddToUserPlayList(form.Result, (LocalTrack)musicDataGrid.SelectedItem);
                OnNewPlaylistAdded(EventArgs.Empty);
            }
        }

        public void SelectIndexAt(int x)
        {
            if (x >= 0 && x<musicDataGrid.Items.Count-1)
            musicDataGrid.SelectedIndex = x;
        }

    
    }

    
}
